<?php
/**
 * Sample of Controller
 */ 
class IndexController extends Zend_Controller_Action{ 
 
    /**
     * @var Zend_View_Wrapper_Smarty 
     */ 
    public $view;
	protected $title;
	function init()
	{
		$this->_helper->viewRenderer->setNoRender(); 
		Zend_Loader::loadClass('Test');
		$this->admin = Zend_Auth::getInstance()->getIdentity();
		//print_r($this->view->user); 
		$this->view = new Smarty_View();
		$this->view->assign("base_url", $this->_request->getBaseUrl()); 
	}
	
	 function preDispatch()
    {
        //die('if you had my love');
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
    }
    public function indexAction()
	{
		$this->title = "He thong quan ly sach ";
		$test = new Test(); ;
		$this->view->assign("title", $this->title);
		//print_r($this->admin);
		$this->view->assign("admin_fullname",$this->admin->admin_fullname);
		if($this->_request->isPost())
		{
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$search_tensach=trim($filter->filter($this->_request->getPost('search_tensach')));
			$all_data=$test->search_data($search_tensach);
			$this->view->assign("troi",$search_tensach);
			$this->view->assign("all_data",$all_data);
			$this->view->render('search.html');
			
		}
		else{
			if($this->admin->admin_type=='a')
			{
				$all_data = $test->getData();
				$this->view->assign("all_data", $all_data);
				$this->view->render('index.html');
			}
			elseif($this->admin->admin_type=='e'){
				$all_data = $test->employee($this->admin->admin_id);
				$this->view->assign("all_data", $all_data);
				$this->view->render('detail_ebook.html');
			}
		}
    }
    public function addAction()
	{   $test=new Test();
	    $this->title = "Cap nhat 1 sach moi";
		$this->view->assign("title", $this->title);
		Zend_Loader::loadClass('Test');
		if ($this->_request->isPost()) {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
        	$name= trim($filter->filter($this->_request->getPost('name')));
        	$author = trim($filter->filter($this->_request->getPost('author')));
        	$description= trim($filter->filter($this->_request->getPost('description')));
        	if($name != '' && $author != '' && $description != '')
				{
					$data=array(
					'name' => $name,
					'author' => $author,
					'description' => $description,
					);
					$test->Insert($data);
					$this->_redirect('/');
				}
		}
    $this->view->render('add.html');
    
	}
	public function editAction()
	{
		$test=new Test();
		
		$this->title = "Sua 1 tai lieu sach";
		$this->view->assign("title",$this->title);
		$id = (int)$this->_request->getParam('id', 0);
		if ($this->_request->isPost()){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			//$id=(int)$this->request->isPost('id');
			$filter = new Zend_Filter_StripTags();
			$name=trim($filter->filter($this->_request->getPost('name')));
			$author=trim($filter->filter($this->_request->getPost('author')));
			$description=trim($filter->filter($this->_request->getPost('description')));
			$admin_id=trim($filter->filter($this->_request->getPost('admin_id')));
			if($id !== false)
			{
				if($name != '' && $author != '' && $description != '')
				{
					$data=array(
					'name' => $name,
					'author' => $author,
					'description' => $description,
					'admin_id' => $admin_id,
					);
					$where = 'id = '.$id;
					$test->Update($data,$where);
					$this->_redirect('/');
				}else {
					$get_edit=$test->edit($id);
					$this->view->assign("get_edit",$get_edit);
				}
			}
		} else {
			if($id > 0)
				{
					$get_edit=$test->edit($id);
					$this->view->assign("aloha",$get_edit);
				} else {
					$this->_redirect('/');
				}
		}
	$this->view->render('edit.html');	
	}
	public function delAction()

	{
		$this->title = "Sua 1 tai lieu sach";
		$this->view->assign("title",$this->title);
		$test = new Test();
		if ($this->_request->isPost()){
		Zend_Loader::loadClass('Zend_Filter_Alpha');
		$filter = new Zend_Filter_Alpha();
		$id = (int)$this->_request->getPost('id');
		$del = $filter->filter($this->_request->getPost('del'));
			if( $del == 'yes' && $id > 0){
					$where = 'id='.$id;
					$test->delete($where);
					$this->_redirect('/');
				} 
		$this->_redirect('/');
		} else {
			$id = (int)$this->_request->getParam('id');
			if($id > 0){
			$get_edit=$test->edit($id);
			$this->view->assign("aloha",$get_edit);
			$this->view->render('del.html');
			}
		}
	}
	
}

?>